
package com.winning.sx.microframework.controller.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.winning.dapi.data.entity.DataTable;
import com.winning.sx.microframework.model.exception.CustomException;
import com.winning.sx.microframework.model.message.AjaxDataResponseMessage;
import com.winning.sx.microframework.model.message.AjaxTableResponseMessage;
import com.winning.sx.microframework.service.EasyService;
import com.winning.sx.microframework.service.sysmanage.ConstService;
import com.winning.sx.microframework.service.sysmanage.RoleManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/base/eqClass")
public class EqClassController {
    @Autowired
    EasyService easyService;
    @Autowired
    RoleManageService service;
    @Autowired
    ConstService services;
   @RequestMapping(value = {"/", ""})
    public ModelAndView init() throws IOException, CustomException, SQLException {
       ModelAndView mv = new ModelAndView("/base/eqClass");
       List state= services.queryCount("200");
       mv.addObject("state",state);
       List eqType= services.queryCount("301");
       mv.addObject("eqType",eqType);
       Map eqRootClassP=new HashMap();
       eqRootClassP.put("superId","0");
       eqRootClassP.put("validFlag","100001");
       DataTable  eqRootClassS = easyService.queryList("BASE_EqClass",eqRootClassP);
       List eqRootClass = eqRootClassS.getDataRows();
       mv.addObject("eqRootClass", eqRootClass);
       return mv;
    }
    @ResponseBody
    @RequestMapping(value = {"/queryList"})
    public AjaxTableResponseMessage queryList(String datas) {
        AjaxTableResponseMessage ajaxTableResponseMessage = new AjaxTableResponseMessage();
        Map<String, String> param = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null)
                param = objectMapper.readValue(datas, Map.class);
            if(param.get("superId")==null){
               throw new Exception("请选择一级分类");
            }
            DataTable all = easyService.querySql("select * from BASE_EqClass w where validFlag='100001' and w.superId=:superId", param);
            ajaxTableResponseMessage.setDatas(all.getDataRows());
            ajaxTableResponseMessage.setTotalSize((int) all.getTotalRowNum());
            ajaxTableResponseMessage.setSuccess(true);
        } catch (Exception e) {
            ajaxTableResponseMessage.setSuccess(false);
            ajaxTableResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxTableResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/save"})
    public AjaxDataResponseMessage insert(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                if(map.get("superId")==null||"0".equals(map.get("superId").toString())){
                    if(map.get("code").toString().length()!=3){
                        throw new Exception("请输入三位数字编号！");
                    }
                    if(map.get("id")!=null&&!"".equals(map.get("id").toString())){
    //有id 判断重复排除本id
                        String sql2="select * from BASE_eqClass where code='" + map.get("code").toString() + "' and id != :id";
                        List entity2= easyService.querySql(sql2,map).getDataRows();
                        if(entity2.size()>0) {
                            throw new Exception("编号重复！");
                        }
                    }else{
                        String sql2="select * from BASE_eqClass where code='" + map.get("code").toString() + "'  ";
                        List entity2= easyService.querySql(sql2,map).getDataRows();
                        if(entity2.size()>0) {
                            throw new Exception("编号重复！");
                        }
                    }

                }else if(map.get("code").toString().length()<6){
                    String sql="select * from BASE_eqClass where superId='" + map.get("superId").toString() + "' order by createTime desc ";
                    List entity= easyService.querySql(sql,map).getDataRows();
                    if(entity.size()>0){
                        Map consts=(Map)entity.get(0);
                        String codeParam=consts.get("code").toString();
                        int code=Integer.parseInt(codeParam);
                        int codeNew=code+1;
                        String codeSave=Integer.toString(codeNew);
                        map.put("code",codeSave);
                    }else{
                        String type=map.get("code").trim().toString();
                        String code=type+"001";
                        map.put("code",code);
                    }
                }
                map.put("state","200001");
                easyService.save("BASE_eqClass", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("操作失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }

    @ResponseBody
    @RequestMapping(value = {"/delete"})
    public AjaxDataResponseMessage delete(String datas) {
        AjaxDataResponseMessage ajaxDataResponseMessage = new AjaxDataResponseMessage();
        Map<String, String> map = null;
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            if (datas != null) {
                map = objectMapper.readValue(datas, Map.class);
                    easyService.delete("BASE_eqClass", "id", map);
                ajaxDataResponseMessage.setSuccess(true);
            }
        } catch (Exception e) {
            ajaxDataResponseMessage.setSuccess(false);
            ajaxDataResponseMessage.setErrorMsg("删除失败：" + e.getMessage());
        }
        return ajaxDataResponseMessage;
    }
}

